﻿using System.Collections.Generic;
using System.Linq;
using Northwind.Interfaces;
using Northwind.Interfaces.Data.Model;
using Northwind.Application.Model;
using Northwind.Business;

namespace Northwind.Application
{
    public class NorthwindManager : INorthwindManager
    {
        private readonly NorthwindServiceRepository _repository;

        public NorthwindManager()
        {
            _repository = new NorthwindServiceRepository();
        }

        //TODO: Why do we need to convert it to List<CustomerModel> before returning
        //TODO: Is it because of deferred execution of linq queries
        public IEnumerable<ICustomerModel> GetAllCustomersNameAndID(
            bool getOrders = false)
        {
            return _repository.GetAllCustomersNameAndID()
                .Select(
                    customerEntity => 
                        {
                            CustomerModel customerModel = new CustomerModel(customerEntity);
                            if (getOrders)
                            {
                                customerModel.Orders = new System.Collections.ObjectModel.ObservableCollection<IOrderModel>(this.GetOrders(customerModel.CustomerID));
                            }
                            return customerModel;
                        }
                        )
                .ToList();
        }

        public IOrderModel GetOrder(string orderID)
        {
            return new OrderModel(_repository.GetOrder(orderID));
        }

        //TODO: Why do we need to convert it to List<CustomerModel> before returning
        //TODO: Is it because of deferred execution of linq queries
        public IEnumerable<IOrderModel> GetOrders(string customerID)
        {
            return _repository.GetOrders(customerID)
                .Select(
                    orderEntity => new OrderModel(orderEntity))
                .ToList();
        }

        //TODO: Why do we need to convert it to List<CustomerModel> before returning
        //TODO: Is it because of deferred execution of linq queries
        public IEnumerable<IOrderDetailModel> GetOrderDetails(int orderID)
        {
            return _repository.GetOrderDetails(orderID)
                .Select(
                    orderDetailsEntity =>
                    new OrderDetailModel(orderDetailsEntity))
                .ToList();
        }
        
        public ICustomerModel GetCustomerByID(string customerID)
        {
            return new CustomerModel(_repository.GetCustomerByID(customerID));
        }

        public IEnumerable<IEmployeeModel> GetEmployees()
        {
            return _repository.GetEmployees().
                        Select(
                            employeeEntity =>
                            {
                                return new EmployeeModel(employeeEntity);
                            }
                        ).ToList();
        }

        public IEnumerable<IProductModel> GetProducts()
        {
           return _repository.GetProducts().
                        Select(
                            productEntity =>
                            {
                                return new ProductModel(productEntity);
                            }
                        ).ToList();
        }

        public void SaveChanges()
        {
            _repository.SaveChanges();
        }
    }
}
